<extend name="Layout:layout_dialog" />
<block name="body_main">
    <form action="{:U('order_add_save')}" method="post" class="xsui-form">
        <fieldset>
            <div class="xsui-form-field">
                <label>用户名：</label>
                <input type="text" name="user_name" placeholder="用户名" value="{$data.user_name}" required="required" data-url="{:U('Member/get_user_item_json','username=varusername')}" />
            </div>
            <div class="xsui-form-field">
                <label>快递单号：</label>
                <input type="text" name="express_no" value="{$data.express_no}" placeholder="快递单号"  required="required" />
            </div>
            <div class="xsui-form-field">
                <label>快递运费：</label>
                <input type="text" name="delivery_fee" value="{$data.delivery_fee}" placeholder="快递运费"  required="required" /> 元
            </div>
            <div class="xsui-form-field">
                <label>订单备注：</label>
                <input type="text" name="buyer_remark" value="{$data.buyer_remark}" placeholder="订单备注" />
            </div>
            <div class="xsui-form-field">
                <label>购物清单：</label>
                <!--<div style="padding:5px 0; color:#F00; font-weight:bold;">共：0 项，0.00 元</div>-->
                <table class="xsui-table-bordered">
                    <tr><th>商品</th><th>数量</th><th>单价</th><th>折扣</th><th>小计</th></tr>
                    <for start="1" end="10">
                        <tr>
                            <td><select name="product_id[]"></select></td>
                            <td><input type="text" name="number[]" size="6" placeholder="数量" value="1" /></td>
                            <td><input type="text" name="price[]" size="6" placeholder="单价" readonly="readonly" /></td>
                            <td><input type="text" name="discount_amount[]" size="6" placeholder="折扣" value="0" /></td>
                            <td>
                                <input type="text" name="subtotal[]" size="6" placeholder="小计" readonly="readonly" />
                                <input type="hidden" name="product_marque[]" />
                                <input type="hidden" name="product_name[]" />
                            </td>
                        </tr>
                    </for>
                </table>
            </div>
        </fieldset>
        <button type="submit" class="ui-state-default"><i class="fa fa-save"></i>保存</button>
        <button type="reset" class="ui-state-default">重置</button>
        <input type="hidden" name="consignee_name" value=""/>
        <input type="hidden" name="consignee_telphone" value=""/>
        <input type="hidden" name="consignee_address" value=""/>
        <input type="hidden" name="buyer_id" value=""/>
        <input type="hidden" name="site_id" value="{$data.site_id}"/>
        <input type="hidden" id="get_product_url" value="{:U('ProductGoods/get_goods_item_json','id=varid')}"/>
        <input type="hidden" id="get_products_url" value="{:U('ProductGoods/get_goods_list_json')}"/>
    </form>    
</block>
<block name="body_after">
    <script type="text/javascript">
        $('[name="user_name"]').blur(function () {
            var $this = $(this);
            var username = $this.val();
            if (!username) {
                return;
            }
            var url = $this.data('url').replace('varusername', username);
            $.get(url, function (data) {
                if (!data) {
                    alert('会员不存在!');
                    $this.val('');
                    return;
                }
                $('[name="consignee_name"]').val(data.consignee_name);
                $('[name="consignee_telphone"]').val(data.consignee_telphone);
                $('[name="consignee_address"]').val(data.consignee_address);
                $('[name="buyer_id"]').val(data.id);
            })
        })

        ajax_combobox({selector: 'select[name="product_id[]"]', url: $('#get_products_url').val(), def_option: '请选择商品'});
        $('select[name="product_id[]"]').change(function () {
            var $this = $(this);
            var $tr = $this.parent().parent();
            var id = $this.val();
            if (!id) {
                $tr.find('[name="price[]"]').val(0);
                $tr.find('[name="product_marque[]"]').val('');
                $tr.find('[name="product_name[]"]').val('');
                calc_subtotal($tr);
                return;
            }
            var url = $('#get_product_url').val().replace('varid', id);
            console.log(url);
            $.get(url, function (data) {
                if (!data) {
                    alert('商品不存在!');
                    $this.val('');
                    return;
                }
                $tr.find('[name="product_marque[]"]').val(data.marque);
                $tr.find('[name="price[]"]').val(data.price);
                $tr.find('[name="product_name[]"]').val(data.title);
                calc_subtotal($tr);
            })
        });
        function calc_subtotal($tr) {
            var number = parseInt($tr.find('[name="number[]"]').val());
            var price = $tr.find('[name="price[]"]').val();
            var discount_amount = parseInt($tr.find('[name="discount_amount[]"]').val());
            var subtotal = number * price - discount_amount;
            $tr.find('[name="subtotal[]"]').val(subtotal);
        }
        $('[name="number[]"], [name="discount_amount[]"]').blur(function () {
            calc_subtotal($(this).parent().parent());
        })
    </script>
</block>